package com.asset.admin.controller;

import com.asset.admin.dao.entity.SysMenuEntity;
import com.asset.admin.system.syemenu.biz.ISysMenuBiz;
import com.asset.admin.system.syemenu.form.MenuForm;
import com.asset.admin.system.syemenu.form.QueryMenuForm;
import com.asset.admin.system.syemenu.form.SysMenuForm;
import com.asset.common.pageform.PageForm;
import com.asset.user.biz.IUserBiz;
import com.asset.user.dao.entity.UserEntity;
import com.asset.user.sevice.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理controller
 * </p>
 *
 * @author 牧之
 * @date 2024/2/27 14:58
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class SysMenuController {

    @Resource
    private ISysMenuBiz menuBiz;

    @Resource
    private IUserService userService;

    @ResponseBody
    @RequestMapping("/getMenu")
    public List<SysMenuForm> getMenu(){
        List<SysMenuEntity> list = menuBiz.getList(null);

        List<SysMenuForm> menuList = list.stream().map(SysMenuForm::convert2MenuForm).collect(Collectors.toList());
        List<SysMenuForm> temp = new ArrayList<>();
        //子节点数据
        menuList.forEach(menu -> {
            List<SysMenuForm> childList = new ArrayList<>();
            menuList.stream().filter(childMenu -> Objects.equals(menu.getKey(), childMenu.getPid())).forEach(childMenu -> {
                childList.add(childMenu);
                temp.add(childMenu);
            });
            menu.setChildren(childList);
        });
        //原数据集合移除子节点
        if (temp.size() > 0) {
            menuList.removeAll(temp);
        }
        return menuList;
    }

    @ResponseBody
    @RequestMapping(value = "/getList" ,method = RequestMethod.GET)
    public PageForm<MenuForm> getList(QueryMenuForm queryMenuForm){
        PageForm<MenuForm> pageForm = new PageForm<>();
        pageForm.setPageNum(queryMenuForm.getPageNum());
        pageForm.setPageSize(queryMenuForm.getPageSize());
        List<SysMenuEntity> list = menuBiz.getList(queryMenuForm);
        List<Long> userIds = list.stream().map(SysMenuEntity::getCreator).filter(Objects::nonNull).collect(Collectors.toList());
        List<UserEntity> userEntities = userService.getUsersByIds(userIds);
        //stream流收集creator信息对应的用户名称map
        Map<Long, String> userIdNameMap = userEntities.stream().collect(Collectors.toMap(UserEntity::getId, UserEntity::getName));
        int total = menuBiz.getCount(queryMenuForm);
        List<MenuForm> menuList = list.stream()
                .map(menu -> MenuForm.convert2MenuForm(menu, userIdNameMap))
                .collect(Collectors.toList());
        pageForm.setTotal(total);
        pageForm.setDataList(menuList);
        return pageForm;
    }

    @ResponseBody
    @RequestMapping(value = "/create" ,method = RequestMethod.POST)
    public void createMenu(@RequestBody MenuForm menuForm){
         menuBiz.createMenu(menuForm);
    }

    @ResponseBody
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    public void updateMenu(@RequestBody MenuForm menuForm) {
        menuBiz.updateMenu(menuForm);
    }


    @ResponseBody
    @RequestMapping(value = "/delete" ,method = RequestMethod.POST)
    public void deleteMenu(@RequestParam("id") Long id) {
        menuBiz.deleteMenu(id);
    }


}
